#!/usr/bin/env python
# -*- coding: utf-8 -*-
#############################
#
# @fileName task.py
# @email:peterliuzy@gmail.com
# @author peter
#
#############################
import sys,urllib,re,md5,os
import MySQLdb
import ConfigParser
import datetime
import time
class task():
    def __init__(self,configFile):
        self.beginTime = time.time()
        self.configFile = configFile
        self.db = MySQLdb.connect(user='root', db='51shanku', passwd='root', host='localhost')
    
    def md(self,str):
        m = md5.new()
        m.update(str)
        return m.hexdigest()
    
    def insert(self,data):
        for row in data:
            keys = self.md(row[1])
            s_sql = "select count(0) as num from user_task where u_space='"+row[1]+"'"
            cursor = self.db.cursor()
            cursor.execute(s_sql)
            a = cursor.fetchall()
            total = a[0][0]
            if total < 1:
                sql = "insert into user_task (u_space,pic,keyes) values ('"+row[1]+"','"+row[0]+"','"+keys+"')"
                cursor = self.db.cursor()
                cursor.execute(sql)
    
    def initConfig(self):
        config = ConfigParser.ConfigParser();
        config.readfp(open(self.configFile));
        return config;
    
    def getUrl(self):
        return
    
    def getHtml(self,domain):
        openTime = time.time()
        print "抓取: %s" % (domain)
        try:
            wp = urllib.urlopen(domain)
        except:
            print 'url exception'
        content = wp.read()
        wp.close()
        print "使用: %s 秒" % (time.time()-openTime)
        return content
    
    def getNode(self,regular,content):
        str = re.compile(regular,re.I|re.S)
        return str.findall(content)
    
    def makes(self,html):
        splitTime = time.time()
        try:
            htmlInfo = html.split("<div class=\"leftWrap\">")[1].split("<div class=\"leaf\">")[0]
        except:
            print "抓取错误"
        print "拆分: %s" % (time.time()-splitTime)
        return htmlInfo
    
    def start(self,pageList):
        config = self.initConfig()
        beginPage = int(config.get("page","beginPage"))
        endPage = int(config.get("page","endPage"))
        domain = config.get("host","showUrl")
        regular = config.get("config","a")
        regularImg = config.get("config","img")
        newRegular = "<img.*?src\=\"(.*?)\"[^>]*><br /><a.*?href\=\"http://space.360quan.com/u/(.*?)\"[^>]*>"
        infoList = []
        for k,v in pageList.items():
            while beginPage <= v:
                pageName = k+"__"+str(beginPage)+".html"
                if (k == 'girl') and (int(beginPage)==1):
                    pageName = ""
                html = self.makes(self.getHtml(domain+pageName))
                infoList = self.getNode(newRegular,html);
                self.insert(infoList)
                beginPage = beginPage+1
                #if beginPage == v:
                #    beginPage = 1
                #    break
                print beginPage,k,v,domain+pageName
                print "完成: %s \n" % (time.time()-self.beginTime)
            beginPage = 1
        self.db.close()

pageList = {"boy":172,"girl":288}
CONFIG_FILE = "../config/_config.ini"
task = task(CONFIG_FILE)
task.start(pageList);
#task.conn('SELECT * FROM user_task ORDER BY id')
#c = task.initConfig()
#t = c.get("host","album");